<template>
  <div>
    <div v-if="!merBusinessList">
      <Error></Error>
    </div>
    <div v-if="merBusinessList">
      <!--业务配置弹框-->
      <div class="dialogFormVisibleDiv">
        <el-dialog title="业务配置" :visible.sync="dialogFormVisible">
          <!--<el-form :model="dialogform" :label-width="formLabelWidth" :rules="rules" ref="dialogform">-->
            <!--<el-form-item label="金融类型" :label-width="formLabelWidth" prop="resource">-->
              <!--<el-radio-group v-model="dialogform.resource" >-->
                <!--<el-radio v-for="(item,key,index) in moneyType" :label="key" :key="index" >{{item}}</el-radio>-->
              <!--</el-radio-group>-->
            <!--</el-form-item>-->
            <!--<el-form-item label="配置银行" :label-width="formLabelWidth" prop="region">-->
              <!--<el-select v-model="dialogform.region" placeholder="请选择银行">-->
                <!--<el-option v-for="(item,index) in applyBankList" :label="item.name" :value="item.code" :key="index"></el-option>-->
              <!--</el-select>-->
            <!--</el-form-item>-->
            <!--<el-form-item label="设置金额" :label-width="formLabelWidth" prop="money">-->
              <!--<el-input v-model="dialogform.money" autocomplete="off" type="number"></el-input>-->
            <!--</el-form-item>-->
          <!--</el-form>-->
          <el-form :model="dialogform" :label-width="formLabelWidth">
              <el-form-item v-for="(item,index) in dataList" :key="index" :label="item.name" :label-width="formLabelWidth" >
                  <el-input v-model="item.value" autocomplete="off" type="text" onkeyup="this.value=this.value.replace(/[^\d.]/g,'')"></el-input>
              </el-form-item>
          </el-form>
          <!--<p v-for="(item,index) in dataList" :key="index">-->
            <!--<el-input class="" v-model="dialogform.key" style="width: 216px" disabled="disabled">{{item.name}}</el-input>-->
            <!--<el-input placeholder="请设置金额" v-model="dialogform.value">{{item.value}}</el-input>-->
          <!--</p>-->
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="configuration('dialogform')">确 定</el-button>
          </div>
        </el-dialog>
      </div>
      <div class="inquireDiv">
        <el-row>
          <el-col :span="6">
            <label>商户号：
              <el-input v-model="form.merchantName" placeholder="请输入商户号"></el-input>
            </label>
          </el-col>
          <el-col :span="6">
            <label>业务类型：
              <el-select v-model="form.merchantBusiness" placeholder="请选择业务">
                <el-option v-for="(item,key,index) in businessData" :label="item" :value="key" :key="index"></el-option>
              </el-select>
            </label>
          </el-col>
          <el-col :span="6">
            <label>状态：
              <el-select v-model="form.merchantState" placeholder="请选择状态">
                <el-option label="全部" value=""></el-option>
                <el-option label="正常" value="1"></el-option>
                <el-option label="禁用" value="2"></el-option>
              </el-select>
            </label>
          </el-col>
          <el-col :span="6" style="text-align: right">
            <el-button type="primary" @click="examineFn">查 询</el-button>
          </el-col>
        </el-row>
      </div>
      <div class="tableDiv" v-loading="loading">
        <el-row>
          <el-table
            :cell-class-name="addClass"
            :data="tableData"
            border
            stripe
            style="width: 100%">
            <el-table-column
              type="index"
              align="center"
              label="#"
              width="48">
            </el-table-column>
            <el-table-column
              prop="mer_no"
              label="商户号"
              align="center"
              width="128">
            </el-table-column>
            <el-table-column
              prop="mer_name"
              label="商户名称"
              align="center"
              width="80">
            </el-table-column>
            <el-table-column
              prop="mb_business_text"
              label="业务类型"
              align="center"
              width="80">  
            </el-table-column>
            <el-table-column
              prop="mb_total_money"
              label="总金额"
              align="center"
              width="100">  
            </el-table-column>
            <el-table-column
              prop="mb_balance_money"
              label="剩余金额"
              align="center"
              width="100">  
            </el-table-column>
            <el-table-column
              label="状态"
              align="center"
              width="60">
              <template slot-scope="scope">
                <span style="color:#F56C6C" v-if="scope.row.mb_status !== '1'">禁用</span>
                <span style="color:#409EFF" v-if="scope.row.mb_status == '1'">正常</span>
              </template>     
            </el-table-column>
            <el-table-column
              prop="mb_create_time"
              sortable
              label="创建时间"
              width="182"
              align="center"
            >
            </el-table-column>
            <af-table-column
              label="操作"
              align="center">
              <template slot="header" slot-scope="scope">
                <div class="el-table-header">
                  <el-button type="primary"  @click.native.prevent="addInformation" style="visibility: hidden">添加</el-button>
                </div>
              </template>
              <template slot-scope="scope">
                <el-button
                  @click.native.prevent="revisionRow(scope.$index,tableData)"
                  type="primary"
                  v-if="merBusinessConfigOne"
                  size="small">
                  查看配置
                </el-button>
                <el-button
                  @click.native.prevent="detailsRow(scope.$index,tableData)"
                  type="warning"
                  v-if="addMerBusinessConfig"
                  size="small">
                  添加配置
                </el-button>
                <el-button v-show="scope.row.mb_status == '1'"
                           v-if="merchantBusinessStatus"
                           @click.native.prevent="deleteRow(scope.$index,tableData,2)"
                           type="danger"
                           size="small">
                  禁用
                </el-button>
                <el-button v-show="scope.row.mb_status !== '1'"
                           v-if="merchantBusinessStatus"
                           @click.native.prevent="deleteRow(scope.$index,tableData,1)"
                           type="success"
                           size="small">
                  启用
                </el-button>
              </template>
            </af-table-column>
          </el-table>
          <el-col :span="24">
            <div class="block" v-if="total">
              <el-pagination
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-size="10"
                layout="total, prev, pager, next"
                :total="total">
              </el-pagination>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
  import Error from '../errorPage/errorPage401'
  export default {
    data() {
      return {
        mid:'',
        mbId:'',
        loading:true,
        currentPage:'',
        mbusiness:'',
        form:{
          merchantName:'',
          merchantState:'',
          merchantBusiness:'',
        },
        dialogform:{
          resource:'',
          region:'',
          money:'',
        },
        applyBankList:[],
        moneyType:{},
        tableData:[],
        dataList:[{
          key:'',
          value:'',
          name:'',
        }],
        businessData:{},
        total:'',
        dialogFormVisible:false,
        formLabelWidth:'120px',
        rules:{
          resource: [
            { required: true, message: '请选择金融类型', trigger: 'change' }
          ],
          region: [
            { required: true, message: '请选择银行', trigger: 'change' }
          ],
          money:[
            { required: true, message: '请设置金额', trigger: 'blur' },
          ]
        },
      //  按钮权限配置
        merchantBusinessStatus:false,
        addMerBusinessConfig:false,
        merBusinessConfigOne:false,
        //页面权限
        merBusinessList:false,
      }
    },
    methods: {
      //初始化
      initFn:function(page){
        let data = {
          page: page,
          mer_no: this.form.merchantName,
          status: this.form.merchantState,
          business: this.form.merchantBusiness,
        };
        let str = this.encryptionAesRsa(data);
        let encryptData = str.split('%$#@zjt@#$%')[0];
        let encryptKey = str.split('%$#@zjt@#$%')[1];
        let incomData = {
          map: 'admin_merchant_business_list',
          plum_session_api: localStorage.getItem('token'),
          encryptData: encryptKey,
          encryptKey: encryptData
        };
        this.$http.post('', incomData).then((data) => {
          if (data.ec == 200) {
            this.loading = false;
            this.total = Number(data.data.count);
            this.currentPage = Number(data.data.page);
            this.tableData = data.data.list;
            this.businessData = data.data.business;
            this.applyBankList = data.data.apply_bank_list;
            this.moneyType = data.data.money_type;
          } else if (data.ec == 401) {
            this.$message.error(data.em);
            setTimeout(() => {
              this.exitLogin();
            }, 1500)
          } else {
            this.$message.error(data.em);
          }
        }).catch((err) => {
          this.$message.error('error!');
        })
      },
      //查询
      examineFn:function () {
        this.initFn(1)
      },
      //列变色
      addClass({row, column, rowIndex, columnIndex}) {
        if (columnIndex === 5) {
          return 'cell-blue'
        }
      },
      //点击分页
      handleCurrentChange(val) {
        this.$router.replace({
          path: this.$route.path,
          query: {
            page: val,
          }
        });
        this.loading = true;
        this.initFn(val);
      },
      //添加业务配置按钮
      detailsRow(index, row){
        this.mid = row[index].mb_mer_id;
        this.mbId = row[index].mb_id;
        // this.mbusiness = row[index].mb_business;
        this.dialogFormVisible = true;
        let data = {
          mb_id:this.mbId,
        };
        let str = this.encryptionAesRsa(data);
        let encryptData = str.split('%$#@zjt@#$%')[0];
        let encryptKey = str.split('%$#@zjt@#$%')[1];
        let incomData = {
          map:'admin_business_info',
          plum_session_api:localStorage.getItem('token'),
          encryptData:encryptKey,
          encryptKey:encryptData
        };
        this.$http.post('',incomData).then((data)=>{
          if(data.ec==200){
            this.dataList = data.data;
            // this.$message.success(data.em);
            // this.dialogFormVisible = false;
            // this.dialogform ={}
          }else if(data.ec==401){
            this.$message.error(data.em);
            setTimeout(()=>{
              this.exitLogin();
            },1500)
          }else {
            this.$message.error(data.em);
          }
        }).catch((err)=>{
          this.$message.error('error!');
        })
      },
      //确定业务配置
      configuration(formName){
        // this.$refs[formName].validate((valid) => {
        //   if (valid) {
        //     let data = {
        //       m_id:this.mid,
        //       business:this.mbusiness,
        //       bank_code:this.dialogform.region,
        //       money_type:this.dialogform.resource,
        //       money:this.dialogform.money,
        //     };
        //     let str = this.encryptionAesRsa(data);
        //     let encryptData = str.split('%$#@zjt@#$%')[0];
        //     let encryptKey = str.split('%$#@zjt@#$%')[1];
        //     let incomData = {
        //       map:'admin_merchant_config_add',
        //       plum_session_api:localStorage.getItem('token'),
        //       encryptData:encryptKey,
        //       encryptKey:encryptData
        //     };
        //     this.$http.post('',incomData).then((data)=>{
        //       if(data.ec==200){
        //         this.$message.success(data.em);
        //         this.dialogFormVisible = false;
        //         this.dialogform ={}
        //       }else if(data.ec==401){
        //         this.$message.error(data.em);
        //         setTimeout(()=>{
        //           this.exitLogin();
        //         },1500)
        //       }else {
        //         this.$message.error(data.em);
        //       }
        //     }).catch((err)=>{
        //       this.$message.error('error!');
        //     })
        //   } else {
        //     return false;
        //   }
        // });
        let list = {};
        let arr = this.dataList;
        // console.log(arr)
        // for(let i=0;i<arr.length;i++){
        //   list[arr[i].key] = arr[i].value;
        // }
        let data = {
          info:JSON.stringify(arr),
          mb_id:this.mbId
        };
        let str = this.encryptionAesRsa(data);
        let encryptData = str.split('%$#@zjt@#$%')[0];
        let encryptKey = str.split('%$#@zjt@#$%')[1];
        let incomData = {
          map:'admin_merchant_config_add',
          plum_session_api:localStorage.getItem('token'),
          encryptData:encryptKey,
          encryptKey:encryptData
        };
        this.$http.post('',incomData).then((data)=>{
          if(data.ec==200){
            this.$message.success(data.em);
            this.dialogFormVisible = false;
          }else if(data.ec==401){
            this.$message.error(data.em);
            setTimeout(()=>{
              this.exitLogin();
            },1500)
          }else {
            this.$message.error(data.em);
          }
        }).catch((err)=>{
          this.$message.error('error!');
        })

      },
      //查看业务配置
      revisionRow(index, row){
        this.$router.push({
          name: 'merBusinessConfigList',
          params: {
            merNo: row[index].mer_no,
            mbBusiness: row[index].mb_business,
          }
        })
      },
      // 禁用/启用按钮
      deleteRow(index, row, status) {
        let str = '';
        if(status == 1){
          str='此操作将启用该用户, 是否继续?'
        }else {
          str='此操作将禁用该用户, 是否继续?'
        }
        this.$confirm(str, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.mid = row[index].mb_id;
          let data = {
            mb_id:this.mid,
            status:status
          };
          let str = this.encryptionAesRsa(data);
          let encryptData = str.split('%$#@zjt@#$%')[0];
          let encryptKey = str.split('%$#@zjt@#$%')[1];
          let incomData = {
            map:'admin_merchant_business_status',
            plum_session_api:localStorage.getItem('token'),
            encryptData:encryptKey,
            encryptKey:encryptData
          };
          this.$http.post('',incomData).then((data)=>{
            if(data.ec==200){
              this.$message.success(data.em);
              this.initFn(this.currentPage)
            }else if(data.ec==401){
              this.$message.error(data.em);
              setTimeout(()=>{
                this.exitLogin();
              },1500)
            }else {
              this.$message.error(data.em);
            }
          }).catch((err)=>{
            this.$message.error('error!');
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          });
        });
      },
    },
    // created() {
    //   this.currentPage = this.$route.query.page ? this.$route.query.page : 1;
    //   this.initFn(this.currentPage);
    // },
    mounted() {
      this.currentPage = this.$route.query.page ? this.$route.query.page : 1;
      let merNo = this.$route.params.merNo;
      merNo = (merNo == null || merNo == "" || typeof (merNo) == "undefined") ? "" : merNo;
      this.form.merchantName = merNo;
      this.initFn(this.currentPage);
      // 按钮权限
      let buttonMenu = localStorage.getItem('buttonMenu');
      buttonMenu = this.edecryptButton(buttonMenu);
      if (buttonMenu.indexOf('merchantBusinessStatus') !== -1) {
        this.merchantBusinessStatus = true
      }
      if (buttonMenu.indexOf('addMerBusinessConfig') !== -1) {
        this.addMerBusinessConfig = true
      }
      if (buttonMenu.indexOf('merBusinessConfigOne') !== -1) {
        this.merBusinessConfigOne = true
      }
      //页面权限
      if (buttonMenu.indexOf('merBusinessList') !== -1) {
        this.merBusinessList = true
      }
    },
    components:{
      Error
    }
  }
</script>

<style scoped>
  .inquireDiv {
    padding: 30px;
    background: #ffffff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04)
  }

  .inquireDiv label {
    display: inline-block;
  }

  .inquireDiv .el-input {
    width: 220px;
  }

  .tableDiv {
    margin-top: 20px;
  }
  .el-col {
    border-radius: 4px;
    height: 75px;
    background-color: #fff;
  }

  .tableDiv >>> .el-pagination {
    height: 30px;
    line-height: 30px;
    margin-top: 25px;
    text-align: center;
  }

  .tableDiv >>> .cell-blue {
    color: #F56C6C !important;
  }
  .drawerDiv{
    width: 100%;
    height: calc(90vh);
    padding: 20px;
    overflow-y: auto;
    box-sizing: border-box;
  }
  .drawerDiv h2{
    text-align: center;
    font-size: 20px;
    margin: 20px 0;
  }
  .drawerDiv p{
    font-size: 16px;
    color: #000000;
    margin-top: 10px;
  }
  .drawerDiv p label{
    font-size: 16px;
    vertical-align: middle;
    font-weight: 600;
    display: inline-block;
    width: 80px;
    text-align: right;
  }
  .drawerDiv p span{
    font-size: 14px;
    vertical-align: middle;
    color: #555555;
    margin-left: 5px;
  }
  .dialogFormVisibleDiv >>>.el-dialog{
    width: 620px;
  }
  .dialogFormVisibleDiv>>>.el-input{
    width: 360px;
  }
  .dialogFormVisibleDiv>>>.el-select{
    width: 360px;
  }
  .el-table-header >>> .el-button {
    float: right;
    height: 40px;
  }
</style>
